package com.huikeportal.controller;

import java.util.Date;
import java.util.List;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.huike.model.MsgResponse;
import com.huike.tools.DateTool;
import com.huike.tools.MessageTool;
import com.huikeportal.model.ReportInfo;
import com.huikeportal.model.common.InterFaceModel;
import com.huikeportal.service.InterFaceCommon;
import com.huikeportal.service.ReportService;

@Controller
public class ReportAPI {

	@Autowired
	private InterFaceCommon interFaceCommon;

	@Autowired
	private ReportService reportService;

	/**
	 * 获取报表列表（个人、推荐）
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/huike_getReportList", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getReportList(String userId) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getReportList");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userId == null || "".equals(userId)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			List<ReportInfo> reportList = reportService.getReportList(userId);
			msgResponse.setObject(reportList);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 获取字典名称
	 * 
	 * @param dicKey
	 * @param dicCode
	 * @param paramType
	 * @param sysLang
	 * @return
	 */
	@RequestMapping(value = "/huike_getBookName", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getBookName(String dicKey, String dicCode, String paramType,
			String sysLang) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getBookName");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		try {
			String rtStr = "";
			if ("0".equals(paramType)) {
				rtStr = reportService.getBookName(dicKey, dicCode, sysLang);
			} else {

				List<String> nameList = reportService.getBookNames(dicKey,
						dicCode, sysLang);
				if (nameList != null) {
					for (int i = 0; i < nameList.size(); i++) {
						rtStr += nameList.get(i).toString() + ",";
					}
					rtStr = rtStr.substring(0, rtStr.length() - 1);
				}
			}
			msgResponse.setObject(rtStr);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 根据报表id查询报表内容
	 * 
	 * @param reportId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/huike_getReportById", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getReportById(String reportId) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getReportById");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (reportId == null || "".equals(reportId)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			ReportInfo reportInfo = reportService.getReportById(Long
					.parseLong(reportId));
			msgResponse.setObject(reportInfo);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 新增报表(用户保存报表)
	 * 
	 * @param userId
	 *            用户id
	 * @param reportName
	 *            报表名称
	 * @param analysisDimension
	 *            分析维度
	 * @param timeType
	 *            时间粒度
	 * @param fromDate
	 *            开始日期
	 * @param toDate
	 *            结束日期
	 * @param indexNum
	 *            指标
	 * @param note
	 *            备注/推荐理由
	 * @return
	 */
	@RequestMapping(value = "/huike_insertReport", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String insertReport(String userId, String reportName,
			String analysisDimension, String timeType, String fromDate,
			String toDate, String indexNum, String note) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_insertReport");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userId == null || "".equals(userId) || reportName == null
				|| "".equals(reportName) || analysisDimension == null
				|| "".equals(analysisDimension) || timeType == null
				|| "".equals(timeType) || fromDate == null
				|| "".equals(fromDate) || toDate == null || "".equals(toDate)
				|| indexNum == null || "".equals(indexNum)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		ReportInfo reportInfo = new ReportInfo();
		reportInfo.setUserId(userId);
		reportInfo.setReportName(reportName);
		reportInfo.setAnalysisDimension(analysisDimension);
		reportInfo.setTimeType(timeType);
		reportInfo.setFromDate(fromDate);
		reportInfo.setToDate(toDate);
		reportInfo.setIndexNum(indexNum);
		reportInfo.setUpdateTime(new Date());
		if (note != null && !"".equals(note)) {
			reportInfo.setNote(note);
		}
		try {
			reportService.insertReport(reportInfo);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 更新报表
	 * 
	 * @param reportId
	 * @param reportName
	 * @param analysisDimension
	 * @param timeType
	 * @param fromDate
	 * @param toDate
	 * @param indexNum
	 * @param note
	 * @return
	 */
	@RequestMapping(value = "/huike_updateReport", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String updateReport(String reportId, String reportName,
			String analysisDimension, String timeType, String fromDate,
			String toDate, String indexNum, String note) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_updateReport");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (reportId == null || "".equals(reportId) || reportName == null
				|| "".equals(reportName) || analysisDimension == null
				|| "".equals(analysisDimension) || timeType == null
				|| "".equals(timeType) || fromDate == null
				|| "".equals(fromDate) || toDate == null || "".equals(toDate)
				|| indexNum == null || "".equals(indexNum)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			ReportInfo reportInfo = new ReportInfo();
			reportInfo.setId(Long.parseLong(reportId));
			reportInfo.setReportName(reportName);
			reportInfo.setAnalysisDimension(analysisDimension);
			reportInfo.setTimeType(timeType);
			reportInfo.setFromDate(fromDate);
			reportInfo.setToDate(toDate);
			reportInfo.setIndexNum(indexNum);
			reportInfo.setNote(note);
			reportInfo.setUpdateTime(new Date());
			reportService.updateReport(reportInfo);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (NumberFormatException e) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 删除报表
	 * 
	 * @param reportId
	 *            报表ID
	 * @return
	 */
	@RequestMapping(value = "/huike_deleteReport", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String deleteReport(String reportId) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_deleteReport");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (reportId == null || "".equals(reportId)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			reportService.deleteReport(Long.parseLong(reportId));
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (NumberFormatException e) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}
}
